<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>应用管理</title>
<link rel="stylesheet" href="/layui/css/layui.css"><script src="/static/js/jquery.min.js"></script><script src="/layui/layui.js"></script>
<style>body{background:#f5f5f5;padding:20px;}</style></head>
<body>
<div class="layui-card"><div class="layui-card-header">应用列表</div><div class="layui-card-body">
<div class="layui-form layui-form-pane" style="margin-bottom:15px;"><div class="layui-inline"><input type="text" id="keyword" placeholder="应用名称/AppID" class="layui-input" value="<?= htmlspecialchars($keyword) ?>"></div><button class="layui-btn layui-btn-primary layui-btn-sm" onclick="search()">搜索</button></div>
<table class="layui-table" lay-size="sm"><thead><tr><th>应用名称</th><th>所属用户</th><th>AppID</th><th>AppKey</th><th>剩余次数</th><th>到期时间</th><th>状态</th><th>操作</th></tr></thead>
<tbody><?php foreach($list as $item): ?><tr><td><?= $item['name'] ?></td><td><?= $item['username'] ?></td><td><?= $item['app_id'] ?></td><td style="max-width:150px;word-break:break-all;"><?= $item['app_key'] ?></td><td><?= $item['remain_calls'] ?></td><td><?= $item['expire_time'] ?: '-' ?></td><td><?= $item['status']?'<span style="color:green">启用</span>':'<span style="color:red">禁用</span>' ?></td><td><button class="layui-btn layui-btn-xs" onclick="resetKey(<?=$item['id']?>)">重置Key</button> <button class="layui-btn layui-btn-xs <?=$item['status']?'layui-btn-danger':'layui-btn-normal'?>" onclick="toggleApp(<?=$item['id']?>)"><?=$item['status']?'禁用':'启用'?></button> <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="deleteApp(<?=$item['id']?>)">删除</button></td></tr><?php endforeach; ?><?php if(empty($list)):?><tr><td colspan="8" style="text-align:center;color:#999;">暂无数据</td></tr><?php endif;?></tbody></table><div id="page"></div></div></div>
<script>
var total=<?=$total?>,page=<?=$page?>,pageSize=<?=$pageSize?>;
layui.use('laypage',function(){var p=layui.laypage;if(total>pageSize){p.render({elem:'page',count:total,limit:pageSize,curr:page,jump:function(o,f){if(!f) location.href='/admin/user/appList?page='+o.curr+'&keyword=<?=urlencode($keyword)?>';}});}});
function search(){location.href='/admin/user/appList?keyword='+encodeURIComponent($('#keyword').val());}
function resetKey(id){layer.confirm('重置AppKey后旧Key将立即失效，确认？',function(){$.post('/admin/user/resetAppKey',{id:id},function(res){layer.msg(res.msg,{icon:res.code===0?1:2});if(res.code===0) setTimeout(function(){location.reload();},1000);});});}
function toggleApp(id){$.post('/admin/user/toggleApp',{id:id},function(res){layer.msg(res.msg);setTimeout(function(){location.reload();},1000);});}
function deleteApp(id){layer.confirm('确认删除？',function(){$.post('/admin/user/deleteApp',{id:id},function(res){layer.msg(res.msg,{icon:res.code===0?1:2});if(res.code===0) setTimeout(function(){location.reload();},1000);});});}
</script></body></html>